package com.web.exiaodao.remote.service.impl;

import com.web.exiaodao.business.constant.JConstant;
import com.web.exiaodao.business.entity.Institution;
import com.web.exiaodao.business.entity.MakeMoneyTable;
import com.web.exiaodao.business.entity.VideoViewRecord;
import com.web.exiaodao.business.repository.InstitutionRepository;
import com.web.exiaodao.remote.service.ApiConfigurationTableService;
import com.web.exiaodao.remote.service.ApiMakeMoneyTableService;
import com.web.exiaodao.remote.vo.ConfigurationTableVo;
import com.web.exiaodao.remote.vo.MakeMoneyTableVo;
import com.web.exiaodao.remote.vo.VideoViewRecordVo;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.web.exiaodao.business.repository.VideoViewRecordRepository;
import com.web.exiaodao.remote.service.ApiVideoViewRecordService;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.service.impl.BaseService;
import com.web.webstart.base.util.result.XaResult;
import com.web.webstart.base.util.XaUtil;

/**
 * @author: zhangl
 * @times: 2016-11-23 12:03:49
 * 类的说明：前端APIVideo_view_record接口实现类
 **/
@Service("ApiVideo_view_recordService")
@Transactional(readOnly = false)
public class ApiVideoViewRecordServiceImpl extends BaseService<VideoViewRecord> implements ApiVideoViewRecordService {

	private static final Logger LOGGER = Logger.getLogger(ApiVideoViewRecordServiceImpl.class);

	@Autowired
	VideoViewRecordRepository video_view_recordRepository;
    @Autowired
    ApiConfigurationTableService configurationTableService;
    @Autowired
    ApiMakeMoneyTableService makeMoneyTableService;
	@Autowired
	InstitutionRepository institutionRepository;

	@Override
	public XaResult<VideoViewRecordVo> findOne(Long tId) throws BusinessException {
		VideoViewRecord obj = video_view_recordRepository.findByIdAndStatusNot(tId,XaConstant.Status.delete);
		XaResult<VideoViewRecordVo> xr = new XaResult<VideoViewRecordVo>();
		if (XaUtil.isNotEmpty(obj)) {
			xr.setObject(JSON.parseObject(JSON.toJSONString(obj),VideoViewRecordVo.class));
			xr.getObject().setId(obj.getId());
			LOGGER.info("查询单个实体----->VideoViewRecord");
		} else {
			xr.error(XaConstant.Message.object_not_find);
			LOGGER.info(XaConstant.Message.object_not_find);
			throw new BusinessException(XaConstant.Message.object_not_find);
		}
		return xr;
	}

	@Override
	public XaResult<VideoViewRecordVo> findLastOne() throws BusinessException {
		throw new BusinessException(XaConstant.Message.error_method_no_impl);
	}

	@Override
	public XaResult<List<VideoViewRecordVo>> findListNEStatusByFilter(
			Integer status, Map<String, Object> filterParams, Pageable pageable)
			throws BusinessException {
		throw new BusinessException(XaConstant.Message.error_method_no_impl);
	}

	@Override
	public XaResult<List<VideoViewRecordVo>> findListEQStatusByFilter(
			Integer status, Map<String, Object> filterParams, Pageable pageable)
			throws BusinessException {
		throw new BusinessException(XaConstant.Message.error_method_no_impl);
	}

	@Override
	public XaResult<VideoViewRecordVo> multiOperate(String modelIds,
													Integer status) throws BusinessException {
		return null;
	}

	@Override
	public XaResult<VideoViewRecordVo> createModel(VideoViewRecord model)
			throws BusinessException {
		XaResult<VideoViewRecordVo> xr = new XaResult<VideoViewRecordVo>();

		Institution institution = institutionRepository.findOne(model.getInstitutionId());
		if (XaUtil.isEmpty(institution)) {
			xr.error(XaConstant.Message.object_not_find);
			return xr;
		}

        //1:增加观看视频记录
		VideoViewRecord obj = video_view_recordRepository.save(model);
		xr.setObject(JSON.parseObject(JSON.toJSONString(obj), VideoViewRecordVo.class));
		xr.getObject().setId(obj.getId());

        //2:观看视频赚到的亲和币 规则
        XaResult<ConfigurationTableVo>  xaResult = configurationTableService.findLastOne();
        if (xaResult == null || xaResult.getObject() == null) {
            xr.error(xaResult.getMessage());
            return xr;
        }
        double makeMoney = xaResult.getObject().getIncome_expenses_watch_viedo_coin() * Long.parseLong(model.getDuration());
        xr.getObject().setConfigurationTableVo(JSON.parseObject(JSON.toJSONString(xaResult.getObject()), ConfigurationTableVo.class));

        //3:赚钱表添加一条数据
        XaResult<MakeMoneyTable> makeMoneyXR = makeMoneyTableService.saveMakeMoneyRecord(model.getUserId(), JConstant.ConsumeOrMakeMoney.MakeMoney, (int) makeMoney, JConstant.IntegralTypeEnum.WATCH_VIDEO.getIndex());
        xr.getObject().setMakeMoneyTableVo(JSON.parseObject(JSON.toJSONString(makeMoneyXR.getObject()), MakeMoneyTableVo.class));

		LOGGER.info("新增数据成功----->VideoViewRecord");
		return xr;
	}

	@Override
	public XaResult<VideoViewRecordVo> updateModel(VideoViewRecord model)
			throws BusinessException {
		return null;
	}

}
